package mytool

import (
	"math"
)

func a___nd(b []int) int {
	a := 0
	for _, i := range b {
		a = i + a
	}
	return a
}
func Duoc(f func([]float64) float64, x []float64, y []float64, tim int, bg int) []float64 {
	a := []int{}
	var t int
	var p []float64
	var pp []float64
	for _, l := range x {
		p = append(p, l)
		pp = append(pp, l)
	}
	for k := 0; k < len(x); k++ {
		a = append(a, bg)
	}
	for i := 0; i < tim; i++ {
		for k := 0; k < len(x); k++ {
			a[k] = bg
		}
	loop:
		copy(pp, x)
		for t = 0; t < len(a); t++ {
			if a[t] != 9 {
				a[t]++
				break
			} else {
				a[t] = bg
			}
		}
		for j := 0; j < len(x); j++ {
			pp[j] = pp[j] + y[j]*math.Pow10(-i)*float64(a[j])
		}
		if f(pp) > f(p) {
			copy(p, pp)
		}
		if a___nd(a) != 0 {
			goto loop
		} else {
			copy(x, p)
		}
	}
	return x
}
